/*
 * @Author: 大白
 * @Date: 2019-12-04 09:48:26
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2020-09-01 09:13:09
 * @Description: 处理全局的面包屑导航
 * @FilePath: \cyf_admin\src\store\modules\home.js
 */
import _ from "lodash"
import {deep} from "@/utils/tools"

const state = {
  isCollapse:false,
  crumbList:[], // 面包屑导航数据
  hisList:[], // 记录用户的历史记录
  
}
const getters = {
  isCollapse:state=>state.isCollapse,
  crumbList:state=>{
    let new_crumbList = []
    deep(state.crumbList,new_crumbList)
    var idx = new_crumbList.filter((v,i)=>{
      return i
    })
    new_crumbList.splice(idx,1)
    return new_crumbList
  },
  hisList:state=>{
    let arr = []
    state.hisList.map(item=>{
      arr.push(item.name)
    })
    return arr.length?arr.toString():""
  }
}
const actions = {
  ADD_HISTORY({ commit }, payload){
    commit('ADD_HISTORYs',payload)
  }
}
const mutations = {
  CHANGE_isCollapse(state){
    state.isCollapse = !state.isCollapse
  },
  SET_CRUMBLIST(state,payload){
    var deep = []
    payload.map(item=>{
      
      deep.push({meta:item.meta,path:item.path})
    })
    state.crumbList = _.cloneDeep(deep)    
  },
  ADD_HISTORYs(state,payload){
    let states = true
    state.hisList.map(item=>{
      if(payload.path==item.path){
        states = false
        item.flag=1
      }else{
        item.flag=""
      }
    })
    if(states&& state.hisList.length<=4){
      payload.meta.is = true
      let {path,name,query,meta} = payload
      state.hisList.push({path,name,query,meta,flag:1})
    }
    // state.hisList.map(item=>{
    //   if(payload.path==item.path){
    //     item.flag=1
    //   }else{
    //     item.flag=""
    //   }
    // })
    // console.log(state.hisList,"yang")
  },
  DEL_TAG(state,payload){
    // console.log(payload)
    state.hisList.splice(payload, 1);
    // console.log(state.hisList)
  },
  DEL_All(state,payload){
    // console.log(payload)
    state.hisList.splice(payload, 1);
    // console.log(state.hisList)
  },
 
}
export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}